<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Muser extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function saveinfoDB()
    {
        $fullname   = $this->getParamString('fullname');
        $address    = $this->getParamString('address');
        $gender     = $this->getParamInt('gender');
        $job        = $this->getParamString('job');
		$birthday        = $this->getParamString('birthday');
        $id         = $this->getParamInt('id');
                
        $data = array(
                'fullname'  => $fullname,
                'job'       => $job,
                'gender'    => $gender,
                'address'   => $address,
				'birthday'	=> $birthday
        );
        $this->db->update('member', $data, "id = '".$id."'");
        if ($this->db->affected_rows() > 0)
        {
            return (object)array('status' => 200, 'description' => 'Cap nhat thanh cong');
        } 
        return (object)array('status' => 201, 'description' => 'Tai khong khong ton tai');
    }
    
    public function loginUserEmail()
    {
        $uname = $this->getParamString('username');
        $upass = $this->getParamString('password');
        $pass_encode = md5(md5($uname).md5($upass));
        
        $sql = $this->db->query("SELECT * FROM member WHERE username = '".strtolower($uname)."' AND password = '".$pass_encode."' ");
        if($sql->num_rows() > 0)
        {
            $data_           = $sql->row();
            $data_->password = null;
            
            $active = $data_->active;
            if($active == 0)
            {
                return (object)array('status' => 203, 'description' => 'Chua kich hoat tai khoan');
            }else if($active == 2)
            {
                return (object)array('status' => 204, 'description' => 'Tai khoan dang bi khoa');
            }else{
                return (object)array('status' => 200, 'content' => $this->getInfoUser($data_->id), 'description' => 'Dang nhap thanh cong');
            }
        }else{
            return (object)array('status' => 201, 'description' => 'Sai tai khoan hoac mat khau');
        }
    }
    
    public function loginUserMobile()
    {
        $mobile = $this->getParamString('mobile');
        $upass = $this->getParamString('password');
        
        $sql = $this->db->query("SELECT * FROM member WHERE mobile = '".$mobile."'");
        if($sql->num_rows() > 0)
        {
            $data_           = $sql->row();
            $pass_encode     = md5(md5($data_->username).md5($upass));
            
            if($pass_encode == $data_->password)
            {
                $active = $data_->active;
                if($active == 0)
                {
                    return (object)array('status' => 203, 'description' => 'Chua kich hoat tai khoan');
                }else if($active == 2)
                {
                    return (object)array('status' => 204, 'description' => 'Tai khoan dang bi khoa');
                }else{
                    return (object)array('status' => 200, 'content' => $this->getInfoUser($data_->id), 'description' => 'Dang nhap thanh cong');
                }
            }else{
                return (object)array('status' => 201, 'description' => 'Sai tai khoan hoac mat khau');
            }
            
        }else{
            return (object)array('status' => 205, 'description' => 'So dien thoai khong ton tai');
        }
    }
    
    
	private function getInfoUser($id)
    {
        $sql = $this->db->query('SELECT id, DATE_FORMAT(dateofbirth, "%d/%m/%Y") AS birthday,mobile,email,address,job, fullname, gender FROM member WHERE id = '.$id);
        $data = $sql->row_object();
        return $data;
    }
	
    public function registerUser()
    {
        $username = $this->getParamString('username');
        $pass = $this->getParamString('password');
        $mobile = $this->getParamString('mobile');
		$fullname   = $this->getParamString('fullname');
        $address    = $this->getParamString('address');
        $gender     = $this->getParamInt('gender');
        $job        = $this->getParamString('job');
		
        $pass_encode = md5(md5($username).md5($pass));
        $qr = $this->db->query('SELECT * FROM member WHERE username = "'.strtolower($username).'" AND mobile = "'.$mobile.'"');
        $check = $qr->num_rows();
        $code = $this->generateCode(5);
        
        if($this->valid_email($username) == 1)
        {
            return (object)array('status' => 100, 'description' => 'Tham so sai kieu gia tri');
        }
        if($check == 0)
        {
            $data = array(
                'username'  => $username,
                'password'  => $pass_encode,
                'email'     => $username,
                'mobile'    => $mobile,
				'fullname'  => $fullname,
                'job'       => $job,
                'gender'    => $gender,
                'address'   => $address,
                'code'      => $code,
				'created'   => date('Y-m-d H:i:s'),
                'active'    => 0
            );
            
            $config = array(
							'protocol' => 'smtp',
							'smtp_host' => 'mail.gvtechcom.vn',
							'smtp_user' => 'info@gvtechcom.vn',
							'smtp_pass' => 'abcde@123',
							'mailtype' => 'html'
                            
                        );
            $this->load->library('email',$config);
            $body    = "<strong>Chào Bạn!</strong> <br /><br /> Bạn vừa đăng ký thành công <br />Để kích hoạt tài khoản này bạn vui lòng nhập mã bên dưới vào form kích hoạt trên App.<br/><br/>=================================<br/>Mã kích hoạt:<b>".$code."</b><br /> Cám ơn bạn !";
            
            $this->email->from('info@gvtechcom.vn', 'Ggift - Kích hoạt tài khoản');
            $this->email->to($username);    
            $this->email->subject('Ggift - Kích hoạt tài khoản');
            $this->email->message($body);    
            
            if($this->email->send()){
                $this->db->insert('member', $data);
                return (object)array('status' => 200, 'description' => 'Dang ky thanh cong');
            }else{
                show_error($this->email->print_debugger());
                //return (object)array('status' => 202, 'description' => 'SMTP dang co loi');
            }
            
        }else{
            return (object)array('status' => 201, 'description' => 'Tai khoan da ton tai');
        }   
    }

    public function infoUser($id)
    {
        $sql = $this->db->query('SELECT DATE_FORMAT(dateofbirth, "%d/%m/%Y") AS birthday, mobile, email, address, job, fullname, gender FROM member WHERE id = '.$id);
        $count = $sql->num_rows();
        $data = $sql->row_object();
        if($count == 1)
        {
            return (object)array('status' => 200,'content' =>  $data, 'description' => 'Thong tin tai khoan');    
        }else{
             return (object)array('status' => 404,'content' =>  null, 'description' => 'Tai khoan khong ton tai');
        }
        
    }
    
    public function changePassDB()
    {
        $uname = $this->getParamString('username');
        $upassold = $this->getParamString('passwordold');
        $upassnew = $this->getParamString('passwordnew');
        $passwordcfr = $this->getParamString('password_confrim');

        $pass_encode = md5(md5($uname).md5($upassold));
        
        $sql = $this->db->query("SELECT * FROM member WHERE username = '".strtolower($uname)."' AND password = '".$pass_encode."' ");
        if($sql->num_rows() > 0)
        {
            if($upassnew != $passwordcfr)
            {
                return (object)array('status' => 201, 'description' => 'Mat khau moi khong khop');
            }
    
            $pass_en_code = md5(md5($uname).md5($upassnew));
            $data = array(
                'password'  => $pass_en_code
            );  
            $this->db->update('member', $data, "username = '".strtolower($uname)."'");
            return (object)array('status' => 200, 'description' => 'Doi mat khau thanh cong');
        }else{
            return (object)array('status' => 500, 'description' => 'Tài khoan hoac mat khau khong dung');
        }

    }
    
    
    public function forGotDB()
    {
        $pass = $this->generateCode(6);
        $uname = $this->getParamString('email');
        $pass_encode = md5(md5($uname).md5($pass));
        
        $qr = $this->db->query('SELECT * FROM member WHERE email = "'.strtolower($uname).'"');
        $check = $qr->num_rows();
        
        if($check > 0)
        {
            $data = array(
                'password'  => $pass_encode
            );  
            $query = $qr->row_object();
            $config = array(
							'protocol' => 'smtp',
							'smtp_host' => 'mail.gvtechcom.vn',
							'smtp_user' => 'info@gvtechcom.vn',
							'smtp_pass' => 'abcde@123',
							'mailtype' => 'html'
                            
                        );
            $this->load->library('email',$config);
            $body    = "<strong>Chào Bạn!</strong> <br /><br /> Mật khẩu mới của bạn là: <b>".$pass."</b> <br />Vui lòng đổi lại mật khẩu sau khi đăng nhập thành công.";
            
            $this->email->from('info@gvtechcom.vn', 'Ggift - Cấp mới mật khẩu');
            $this->email->to($query->email);    
            $this->email->subject('Ggift - Cấp mới mật khẩu');
            $this->email->message($body);    
            
            if($this->email->send()){
                $this->db->update('member', $data, "email = '".strtolower($uname)."'");
                return (object)array('status' => 200, 'description' => 'Lay lai mat khau thanh cong');
            }else{
                return (object)array('status' => 201, 'description' => 'SMTP dang co loi');
            }
            
        }else{
            return (object)array('status' => 404, 'description' => 'Tai khoan khong ton tai');
        }
    }
    
    public function verifyaccountDB()
    {
        $id = $this->getParamInt('id');
        $code = $this->getParamString('code');
        
        $sql = $this->db->query("SELECT * FROM member WHERE code = '".$code."' AND id = ".$id);
        if($sql->num_rows() > 0)
        {
            $data = array(
                'active'  => 1
            );  
            $this->db->update('member', $data, "id = ".$id);
            return (object)array('status' => 200, 'description' => 'Kich hoat thanh cong');
        }else{
            return (object)array('status' => 201, 'description' => 'Sai ma kich hoat');
        }
    }
    
    public function registerFacebookDB()
    {
        $username = $this->getParamString('username');
        $email = $this->getParamString('email');
        $pass = $this->generateCode(6);
        $pass_encode = md5(md5($username).md5($pass));
        $qr = $this->db->query('SELECT * FROM member WHERE username = "'.strtolower($username).'"');
        $check = $qr->num_rows();
        $code = $this->generateCode(5);
        
        
        if($check == 0)
        {
            $data = array(
                'username'  => $username,
                'password'  => $pass_encode,
                'email'     => $username,
                'fullname'  => $this->getParamString('fullname'),
                'code'      => $code,
                'active'    => 1,
                'fbid'      => $this->getParamString('fbid')
            );
            $this->db->insert('member', $data);
			$ID = $this->db->insert_id();
            return (object)array('status' => 200, 'content' => $this->getInfoUser($ID), 'description' => 'Dang nhap thanh cong');
        }else{
			$data_           = $qr->row();
            return (object)array('status' => 200, 'content' => $this->getInfoUser($data_->id), 'description' => 'Dang nhap thanh cong');
        }   
    }
    
}
